﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace BusinessLogicLibrary
{
    public static class Utility
    {
        public static string CreateAccount(string name)
        {
            name = ConvertToUnsign(name);
            string[] temp = name.Split(' ');
            int lastidx = temp.Count() - 1;
            string emId = temp[lastidx];
            string lastemId = "";
            for (int i = 0; i < lastidx; i++)
            {
                if (temp[i].Trim() != "")
                {
                    lastemId = lastemId + temp[i];
                }
            }
            if (lastemId.Trim() != "")
            {
                emId = emId + "." + lastemId;
            }
            return emId;
        }


        public static string ConvertToUnsign(string str)
        {
            string[] pattern = {
                                   "(áàảãạăắằẳẵặâấầẩẫậ)", "đ", "(éèẻẽẹêếềểễệ)", "(íìỉĩị)", "(óòỏõọôốồổỗộơớờởỡợ)",
                                   "(úùủũụưứừửữự)", "(ýỳỷỹỵ)"
                               };
            char[] map = {'a', 'd', 'e', 'i', 'o', 'u', 'i'};
            char[] strChar = str.ToLower().ToCharArray();

            for (int i = 0; i < pattern.Count(); i++)
            {
                for (int j = 0; j < strChar.Count(); j++)
                {
                    if (pattern[i].IndexOf(strChar[j]) > -1)
                    {
                        strChar[j] = map[i];
                    }
                }
            }

            return new string(strChar);
        }

        public static string CreateRandomPassword(int passwordLength)
        {
            const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            var chars = new char[passwordLength];
            var rd = new Random();

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }

            return new string(chars);
        }

        public static bool validPhoneEmail(string phone, string newemail, out string err)
        {
            var rePhone = new Regex(@"^0+\d{2,3}-\d{3}-\d{4}$");
            if (!rePhone.IsMatch(phone))
            {
                err = "Phone number mus be valid format." + "0xx[x]-xxx-xxxx";
                return false;
            }
            int idxA = newemail.IndexOf("@", StringComparison.Ordinal);
            if (idxA == -1 || newemail.IndexOf(".", idxA, StringComparison.Ordinal) <= idxA)
            {
                err = "e-mail address must be valid e-mail address format. For example 'someone@example.com' ";
                return false;
            }
            err = "";
            return true;
        }

        public static byte[] FileToByteArray(string fileName)
        {
            // Open file for reading
            var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // attach filestream to binary reader
            var binaryReader = new BinaryReader(fileStream);

            // get total byte length of the file
            long totalBytes = new FileInfo(fileName).Length;

            // read entire file into buffer
            byte[] buffer = binaryReader.ReadBytes((Int32) totalBytes);

            // close file reader
            fileStream.Close();
            fileStream.Dispose();
            binaryReader.Close();

            return buffer;
        }
    }
}